/* Andreas Baum
 * ClientThreadPair.java
 * Handles the interaction of the server proxy with the client proxy
 * CS 78 - Networks
 */

/* The proxy allows several users to connect to it */

/* 
 * The notion of "server" in this project can lead to confusion: 
 * Indeed, it is employed with two separate meanings.
 * 1. The central login server
 * 2. The "server" side of the iTunes connection to which another proxy connect
 * , and which allows another user to access the user's iTunes library.
 */

package jbpServerConnector;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import jbpClientConnector.DataThread;
import jbpClientConnector.JBPClientConnector;

public class ServerThreadPair{

	public static int SERVER_PORT = JBPClientConnector.SERVER_PORT;
	public static int ITUNES_PORT = 3689;

	private Socket iTunesSocket;
	private Socket extSocket;
	private DataThread sExtThread;
	private DataThread iTunesDataThread;

	public ServerThreadPair(Socket externSocket) throws IOException {

		extSocket = externSocket;

		// Create a thread to handle communication with a client proxy:
		sExtThread = new DataThread(extSocket, "sExtThread");
		
		iTunesSocket = null;
		try {
			iTunesSocket = new Socket("localhost", ITUNES_PORT);
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			System.err.println("Unknown host: localhost.");
			e1.printStackTrace();
			System.exit(1);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			System.exit(1);
		}

		iTunesDataThread = new DataThread(iTunesSocket, "iTunesDataThread");
		
		iTunesDataThread.setOtherThread(sExtThread);
		sExtThread.setOtherThread(iTunesDataThread);


	}

}
